package com.mytaobao.main.firsttab.func.menu;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baseproject.imageload.ImageLoaderUtil;
import com.mytaobao.R;
import com.tmall.wireless.tangram.structure.BaseCell;
import com.tmall.wireless.tangram.structure.view.ITangramViewLifeCycle;

/**
 * Created by hp on 2017/7/4.
 */

public class MenuItemView extends LinearLayout implements ITangramViewLifeCycle{

    private ImageView ivIcon;
    private TextView tvText;
    private BaseCell baseCell;

    public MenuItemView(Context context) {
        super(context);
        init();
    }

    public MenuItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MenuItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.view_menu_item, this);
        setGravity(Gravity.CENTER);
        setOrientation(VERTICAL);
        ivIcon = (ImageView) findViewById(R.id.iv_icon);
        tvText = (TextView) findViewById(R.id.tv_text);
    }

    @Override
    public void cellInited(BaseCell cell) {
        baseCell = cell;
    }

    @Override
    public void postBindView(BaseCell cell) {
        //此句调用会在返回退出应用之后再次进入应用时，出现调用已经销毁的activity的异常，因为context已经被销毁，属于tangram框架bug
//        ImageUtils.doLoadImageUrl(ivIcon, cell.optStringParam("icon"));
        ImageLoaderUtil.getInstance().with(getContext()).load(cell.optStringParam("icon")).into(ivIcon);
        tvText.setText(cell.optStringParam("text"));
    }

    @Override
    public void postUnBindView(BaseCell cell) {

    }
}
